﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PathologicalGames;

namespace GameJam
{
	[RequireComponent(typeof(SpriteRenderer))]
	public class Bullet : MonoBehaviour 
	{
		[SerializeField]
		float _moveSpeed;
		[SerializeField]
		public string poolName = "Bullet";

		TreePlayer _owner;

		public TreePlayer Owner 
		{
			get{ return _owner;}
			set{ _owner = value;}
		}

		public float MoveSpeed
		{
			set{ _moveSpeed = value;}
		}

		ParticleSystem _particle;
		SpriteRenderer _sprite;
		// Use this for initialization
		void Start () 
		{
			_particle = this.GetComponentInChildren<ParticleSystem> ();
			_sprite = this.GetComponent<SpriteRenderer> ();
		}
		
		// Update is called once per frame
		void Update () 
		{
			Vector3 newPos = this.transform.position;
			newPos += this.transform.up * _moveSpeed * Time.deltaTime;
			this.transform.position = newPos;

			//_mesh.material.color = _color;
		}

		void OnTriggerEnter2D(Collider2D other)
		{
			if (other.gameObject.tag == "Wall") {
				SpawnPool shapesPool = PoolManager.Pools [poolName];
				if(this.gameObject.activeSelf) shapesPool.Despawn (this.transform);
			}
		}

		public void SetColor(Color color)
		{
//			if(_sprite == null)
//				_sprite = this.GetComponent<SpriteRenderer> ();
//			_sprite.color = color;

			if(_particle == null)
				_particle = this.GetComponentInChildren<ParticleSystem> ();
			ParticleSystem.MainModule mm = _particle.main;
			mm.startColor = color;
		}
	}
}
